package org.ruixue.controller;


import cn.rkylin.oms.common.dto.DBLog;
import com.alibaba.fastjson.JSON;

import org.ruixue.request.ConsignShipOrderRequest;
import org.ruixue.response.BaseResponse;
import org.ruixue.service.ShipService;
import org.ruixue.util.LogUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@EnableAutoConfiguration
@RequestMapping("/ship")
public class ShipController {

    @Autowired
    LogUtil logUtil;

    @Autowired
    ShipService shipService;

    /**
     * http://localhost:40005/microservice-yiyouyayin/ship/consignYiShipOrder?shopId=55ed507f052747a0aa97d56d63f53f99
     * 平台发货 按店铺
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/consignYiShipOrder", method = RequestMethod.GET)
    private ResponseEntity<BaseResponse> consignYiShipOrder(@RequestParam String shopId) {
        BaseResponse response = new BaseResponse();
        try {
            response = shipService.consignSuningShipOrder(shopId);
            logUtil.info(new DBLog(shopId, "", "consignYiShipOrder", JSON.toJSONString(response)));
        } catch (Exception e) {
            e.printStackTrace();
            response.setMsg(e.getMessage());
            response.setSuccess("false");
            logUtil.error(new DBLog(shopId, "", "consignSuningShipOrder", e.getMessage()), e);
        }
        return new ResponseEntity<BaseResponse>(response, HttpStatus.OK);
    }

    /**
     * http://localhost:40005/microservice-yiyouyayin/ship/consignSuningShipOrder?shopId=55ed507f052747a0aa97d56d63f53f99
     * 平台发货 按发货单
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/consignYiShipOrder", method = RequestMethod.POST)
    private ResponseEntity<BaseResponse> consignYiShipOrder(@RequestParam String shopId, @RequestBody ConsignShipOrderRequest consignShipOrderRequest) {
        BaseResponse response = new BaseResponse();
        try {
            response = shipService.consignSuningShipOrder(shopId, consignShipOrderRequest);
            logUtil.info(new DBLog(consignShipOrderRequest.getEcTradeCode(), "", "consignYiShipOrder", JSON.toJSONString(response)));
        } catch (Exception e) {
            e.printStackTrace();
            response.setMsg(e.getMessage());
            response.setSuccess("false");
            logUtil.error(new DBLog(consignShipOrderRequest.getEcTradeCode(), "", "consignYiShipOrder", e.getMessage()), e);
        }
        return new ResponseEntity<BaseResponse>(response, HttpStatus.OK);
    }
}
